package com.shengyuan.manage_os.old.ying.boce.jsy;

import com.shengyuan.manage_os.old.ying.erpm.db.DBHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;


public class Db_carphone {
	String CARPHONE="car_phone";
	private Context mContext;
	int pID;
	public void CheckDataBase(Context context) {
		mContext = context;
		DBHelper dbHelper = new DBHelper( context );
		dbHelper.open();
		try {
				if (!dbHelper.isTableExist(CARPHONE)) {
					String sql = "CREATE TABLE "+ CARPHONE +" (id integer primary key autoincrement, car text, phone text)";    
					dbHelper.execSQL(sql);
				}
		} catch (Exception e) {
			String s = e.getMessage();
		}
		dbHelper.close();
//		try {
//			insert("mm", "13702170721");
//			selectInfo("mm");	
//			update("mm", "13131");
//			delete();
//		} catch (Exception e) {
//			// TODO: handle exception
//		}
	}
	
	public void AddPhone(String car, String phone) {
		selectInfo(car);
		if (pID==0) {
			insert(car, phone);
		}else {
			update(car, phone);
		}
	}
	
    public void insert(String car, String phone) {  
    	try {
	        DBHelper dbHelper = new DBHelper(mContext);  
	        dbHelper.open();  
	          
	        ContentValues values =  new ContentValues();  //相当于map   
	          
	        values.put("car", car);  
	        values.put("phone", phone);  
	  
	        dbHelper.insert(CARPHONE, values);  
	        dbHelper.close();			
		} catch (Exception e) {
		}
    } 
    
    public void update(String car, String phone) {  
    	try {
	        DBHelper dbHelper = new DBHelper(mContext);  
    	    dbHelper.open();  
			ContentValues initialValues = new ContentValues();  
			initialValues.put("car", car);  
			initialValues.put("phone", phone);
			dbHelper.update(CARPHONE, initialValues, "id = '"+ pID +"'", null);   //第三个参数为 条件语句   
			dbHelper.close();  			
		} catch (Exception e) {
		}
  
    }  
    public void delete() throws Exception{  
        DBHelper dbHelper = new DBHelper(mContext);  
        dbHelper.open();  
        dbHelper.delete(CARPHONE, "id = '"+ pID +"'", null);  
        dbHelper.close();  
    }  
	public String selectInfo(String car){  
		String phone="";
		try {
	        DBHelper dbHelper = new DBHelper( mContext );  
	        dbHelper.open();  
	        Cursor returnCursor = dbHelper.findList( CARPHONE ,new String[] {"id","car", "phone"}, "car = '"+ car +"'", null,null, null, "id desc");
	        pID = 0;
	        if (returnCursor.getCount() > 0) {  
	            returnCursor.moveToFirst();
	            pID = returnCursor.getInt(returnCursor.getColumnIndexOrThrow("id"));
	            phone = returnCursor.getString(returnCursor.getColumnIndexOrThrow("car"));  
	            phone = returnCursor.getString(returnCursor.getColumnIndexOrThrow("phone"));  
	        } 			
		} catch (Exception e) {
			String s =e.getMessage();
		}
 
        return phone;
    }  

}
